package com.ruoyi.project.jiaowu.kaikeplan.service.impl;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.jiaowu.kaikeplan.domain.TBKaiKePlanResult;
import com.ruoyi.project.jiaowu.kaikeplan.domain.TBKaikePlanParam;
import com.ruoyi.project.jiaowu.kaikeplan.domain.TbKaikePlan;
import com.ruoyi.project.jiaowu.kaikeplan.domain.UserPlan;
import com.ruoyi.project.jiaowu.kaikeplan.mapper.TbKaikePlanMapper;
import com.ruoyi.project.jiaowu.kaikeplan.service.ITbKaikePlanService;
import com.ruoyi.project.jiaowu.term.service.ITbTermService;
import com.ruoyi.project.jiaowu.term.service.TermService;
import com.ruoyi.project.jiaowu.totalcoursemain.domain.TbTotalCourseMain;
import com.ruoyi.project.jiaowu.totalcoursemain.mapper.TbTotalCourseMainMapper;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 开课计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2019-10-23
 */
@Service
public class TbKaikePlanServiceImpl implements ITbKaikePlanService
{
    @Autowired
    private TbKaikePlanMapper tbKaikePlanMapper;

    @Autowired
    private TbTotalCourseMainMapper tbTotalCourseMainMapper;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private ITbTermService iTbTermService;

    /**
     * 查询开课计划
     * 
     * @param id 开课计划ID
     * @return 开课计划
     */
    @Override
    public TbKaikePlan selectTbKaikePlanById(Long id)
    {
        return tbKaikePlanMapper.selectTbKaikePlanById(id);
    }

    /**
     * 查询开课计划列表
     * 
     * @param tbKaikePlan 开课计划
     * @return 开课计划
     */
    @Override
    public List<TBKaiKePlanResult> selectTbKaikePlanList(TBKaikePlanParam tbKaikePlan)
    {
        return tbKaikePlanMapper.selectTbKaikePlanList(tbKaikePlan);
    }

    /**
     * 新增开课计划
     * 
     * @param tbKaikePlan 开课计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTbKaikePlan(TbKaikePlan tbKaikePlan, UserPlan userPlan)
    {
        //获取当前时间
        tbKaikePlan.setCreateTime(DateUtils.getNowDate());
        //获取学时学分
        tbKaikePlan.setStutime(tbKaikePlanMapper.findStudyTime(tbKaikePlan.getCourseId()));
        tbKaikePlan.setStuscore(tbKaikePlanMapper.findStudyScore(tbKaikePlan.getCourseId()));
        //添加开课计划
        return tbKaikePlanMapper.insertTbKaikePlan(tbKaikePlan);
        //返回主键信息，设置为中间表的id
        //userPlan.setCourseId(tbKaikePlan.getId());
        //return tbKaikePlanMapper.insertPlanUser(userPlan);
    }

    /**
     * 修改开课计划
     * 
     * @param tbKaikePlan 开课计划
     * @return 结果
     */
    @Override
    public int updateTbKaikePlan(TbKaikePlan tbKaikePlan)
    {
        tbKaikePlan.setUpdateTime(DateUtils.getNowDate());
        return tbKaikePlanMapper.updateTbKaikePlan(tbKaikePlan);
    }

    /**
     * 删除开课计划对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTbKaikePlanByIds(String ids)
    {
        return tbKaikePlanMapper.deleteTbKaikePlanByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除开课计划信息
     * 
     * @param id 开课计划ID
     * @return 结果
     */
    @Override
    public int deleteTbKaikePlanById(Long id)
    {
        return tbKaikePlanMapper.deleteTbKaikePlanById(id);
    }

    /**
     * 导入开课计划
     * @param tbKaikePlans
     * @param isUpdateSupport
     * @return
     */
    @Override
    public String importTbKaikePlan(List<TbKaikePlan> tbKaikePlans, Boolean isUpdateSupport) {
        if (StringUtils.isNull(tbKaikePlans) || tbKaikePlans.size() == 0)
        {
            throw new BusinessException("导入开课计划数据不能为空！");
        }
        //System.err.println(tbKaikePlans);
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TbKaikePlan t: tbKaikePlans)
        {
            //获取当前时间
            t.setCreateTime(DateUtils.getNowDate());
            //获取学时学分
            t.setStutime(tbKaikePlanMapper.findStudyTime(t.getCourseId()));
            t.setStuscore(tbKaikePlanMapper.findStudyScore(t.getCourseId()));
            /**
             * 判断当前导入的开课计划包含的课程名称是否在总课程管理里面
             */
            TbTotalCourseMain tbTotalCourseMain=tbTotalCourseMainMapper.selectTbTotalCourseMainById(t.getCourseId());
            /**
             * 获取所有教师信息
             */
            List<User> users=iUserService.getUserName();
            int status=0;
            for(User u:users){
                if(u.getUserName().equals(t.getCourseTeacher())){
                    status=1;
                }
            }
            try
            {
                if (t!=null && tbTotalCourseMain!=null && status==1)
                {
                    t.setCreateTime(new Date());
                    /**
                     * 设置学期数
                     */
                    t.setStuterm(Integer.valueOf(iTbTermService.selectTbTermById(t.getStuYear()).getTerm()));
                    tbKaikePlanMapper.insertTbKaikePlan(t);
                    successNum++;
                    successMsg.append("<br/>" + successNum + " 、课程 " + t.getKaikeName() + " 导入成功");
                }
                else
                {
                    if(tbTotalCourseMain == null){
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、总课程id" + t.getCourseId() + " 不存在,请填写正确id");
                    }else if(status==0){
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、教师名称" + t.getCourseTeacher() + " 不存在,请填写正确名称");
                    }
                    else{
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、课程 " + t.getKaikeName() + " 已存在");
                    }
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、课程 " + t.getKaikeName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int fabu(String id) {
        return tbKaikePlanMapper.fabuPlans(id);
    }

    @Override
    public int fabuPlan(int param,String id) {
        return tbKaikePlanMapper.fabuPlan(id);
    }
}
